package com.jwell.panrank.ui.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jwell.panrank.R;
import com.jwell.panrank.ui.adapter.LicenseChooseAdapter;

/**
 * 车牌选择器
 */

public class LicensePlateUtils implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater mInflater;
    private PopupWindow mPopupWindow;
    private RecyclerView mRecycleView;
    private TextView mCancel;//取消
    private TextView mSelect;//车牌显示
    private TextView mEnsure;//确定

    private StringBuffer mStringBuffer;

    private String[] mCities;
    private String[] mLicenseNum;

    private LicenseSelectedListener mListener;
    private LicenseChooseAdapter mAdapter;

    public LicensePlateUtils(Context context, PopupWindow popupWindow) {
        mContext = context;
        mPopupWindow = popupWindow;
        mInflater = LayoutInflater.from(mContext);
    }

    /**
     * 显示在父控件下方
     *
     * @param v
     */
    public void showInParentDown(View v) {
        getPopupWindowInstance();
        mPopupWindow.showAsDropDown(v);
    }

    private void getPopupWindowInstance() {
        if (null == mPopupWindow) {
            initLicense();
        }
    }

    private void initLicense() {
        View popupWindow = mInflater.inflate(R.layout.license_chooser, null);
        mRecycleView = (RecyclerView) popupWindow.findViewById(R.id.rv_license_chooser);
        mCancel = (TextView) popupWindow.findViewById(R.id.tv_license_cancel);
        mSelect = (TextView) popupWindow.findViewById(R.id.tv_license_select);
        mEnsure = (TextView) popupWindow.findViewById(R.id.tv_license_ensure);
        mCities = new String[]{"川", "云", "贵", "渝", "京", "津", "沪", "冀", "豫", "辽", "黑", "湘", "皖", "闽", "鲁", "新", "苏", "浙", "赣", "鄂", "桂", "甘", "晋", "蒙", "陕", "吉", "粤", "青", "藏", "宁", "琼"};
        mLicenseNum = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

        mCancel.setOnClickListener(this);
        mEnsure.setOnClickListener(this);

        GridLayoutManager manager = new GridLayoutManager(mContext, 5);
        mRecycleView.setLayoutManager(manager);
        mPopupWindow = new PopupWindow(popupWindow, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), (Bitmap) null));


        setListener();
    }

    private void setListener() {
        LicenseChooseAdapter adapter = new LicenseChooseAdapter(mContext, mCities, null);
        mAdapter = adapter;
        mRecycleView.setAdapter(adapter);
        adapter.setOnLicenseCliskListener(new LicenseChooseAdapter.OnLicenseClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mEnsure.setVisibility(View.VISIBLE);
                if (mStringBuffer == null) {
                    mStringBuffer = new StringBuffer();
                } else {
                    mStringBuffer = null;
                }

                mStringBuffer.append(mCities[position]);
                mSelect.setText(mStringBuffer);
                LicenseChooseAdapter licenseNumAdapter = new LicenseChooseAdapter(mContext, null, mLicenseNum);
                mRecycleView.setAdapter(licenseNumAdapter);
                licenseNumAdapter.setOnLicenseCliskListener(new LicenseChooseAdapter.OnLicenseClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        mStringBuffer.append(mLicenseNum[position]);
                        mSelect.setText(mStringBuffer);
                        if (mStringBuffer.length() >= 2) {
                            if ((mStringBuffer.charAt(1) >= 'A') && (mStringBuffer.charAt(1) <= 'Z')) {
                                if (mStringBuffer.length() == 7) {
                                    if (mListener != null) {
                                        mListener.onLicenseSelected(String.valueOf(mStringBuffer));
                                        closePopupWindow();
                                    }
                                }
                            } else {
                                if (mStringBuffer.length() == 8) {
                                    if (mListener != null) {
                                        mListener.onLicenseSelected(String.valueOf(mStringBuffer));
                                        closePopupWindow();
                                    }
                                }
                            }
                        }

//                        if (mStringBuffer.length() < 7) {
//                            if (mStringBuffer.length() == 1) {
//                                if (!((mLicenseNum[position].charAt(0) >= 'A') && (mLicenseNum[position].charAt(0) <= 'Z'))) {
//                                    Toast.makeText(mContext, "请输入正确的车牌号！", Toast.LENGTH_LONG).show();
//                                    return;
//                                }
//                            }
//                            mStringBuffer.append(mLicenseNum[position]);
//                            mSelect.setText(mStringBuffer);
//                            if (mStringBuffer.length() == 7) {
//                                if (mListener != null) {
//                                    mListener.onLicenseSelected(String.valueOf(mStringBuffer));
//                                    closePopupWindow();
//                                }
//                            }
//                        }
                    }
                });
            }
        });
    }

    /**
     * 关闭popupwindow
     */
    public void closePopupWindow() {
        if (null != mPopupWindow && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_license_cancel:
                closePopupWindow();
                break;
            case R.id.tv_license_ensure:
                if (null != mStringBuffer && mStringBuffer.length() > 1) {//选择车牌后回退重新选择
                    mStringBuffer.deleteCharAt(mStringBuffer.length() - 1);
                    mSelect.setText(mStringBuffer);
                } else {
                    mEnsure.setVisibility(View.GONE);
                    mStringBuffer = null;
                    mSelect.setText("未选择");
                    mRecycleView.setAdapter(mAdapter);
                }
//                if (mStringBuffer != null) {
//                    if (mStringBuffer.length() < 7 && mStringBuffer.length() > 0) {
//                        ToastUtil.showDesignToast(mContext, "请输入正确的车牌号！", 3000);
//                    } else {
//                        if (mListener != null) {
//                            mListener.onLicenseSelected(String.valueOf(mStringBuffer));
//                            closePopupWindow();
//
//
//                        }
//                    }
//                }
                break;
        }
    }


    public interface LicenseSelectedListener {
        void onLicenseSelected(String licenseNum);
    }

    public void setLicenseSelectedListener(LicenseSelectedListener listener) {
        mListener = listener;
    }
}
